package com.iman.Confucius.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.iman.Confucius.domain.User;
import com.iman.Confucius.domain.UserSInfo;
import com.iman.Confucius.service.UserService;
import com.iman.common.hibernate.HibernateSupport;
import com.iman.common.hibernate.TemplateQLService;

/**
 * *************************************************************************
 * @文件名称: UserServiceImpl.java
 *
 * @包路径  : com.iman.Confucius.service.impl 
 *				 
 * @版权所有:  科技有限公司 (C) 2014
 *
 * @类描述:  
 * 
 * @创建人:   wangxiaolei  
 *
 * @创建时间: 2014年12月25日 - 下午3:05:16 
 *
 * @修改记录:
   -----------------------------------------------------------------------------------------------
             时间						|		修改人		|		修改的方法		|		修改描述                                                                
   -----------------------------------------------------------------------------------------------
							|					|					|                                       
   ----------------------------------------------------------------------------------------------- 	
 
 **************************************************************************
 */

@Service("userService")
public class UserServiceImpl  implements UserService{

	@Inject
	private TemplateQLService templateQLService;

    @Inject
    private HibernateSupport  hibernateSupport;

 

	/**
	 * 增加学分
	 */
	@Override
	public boolean addUserCredit(long userid, int creditCnt) {
		if(userid <1 ||  creditCnt <1 ){
			return false;
		}
		 
		Map params =  new HashMap();
		params.put("user_id", userid);
		params.put("user_credit", creditCnt);
		int result =  (Integer) this.templateQLService.executeSQLUpdate( "user-addUserCredit", params);
		return result>0?true:false;
	}

	/**
	 * 减少学分
	 */
	@Override
	public boolean reduceUserCredit(long userid, int creditCnt) {
		if(userid <1 ||  creditCnt <1 ){
			return false;
		}
		Map params =  new HashMap();
		params.put("user_id", userid);
		params.put("user_credit", creditCnt);
		int result =  (Integer) this.templateQLService.executeSQLUpdate("user-reduceUserCredit", params);
		return result>0?true:false;
	}

	@Override
	public UserSInfo getUserSInfo(long userId) {
		Map params =  new HashMap();
		params.put("user_id", userId);
		List<UserSInfo> usersinfo = this.templateQLService.findBySQLBeanQuery("user-getsinfo", params, UserSInfo.class);
		if(usersinfo != null && usersinfo.size() >0){
			return usersinfo.get(0);
		}else {
			return null;
		}
	}

	@Override
	public User getUser(long userId) {
		Map params =  new HashMap();
		params.put("user_id", userId);
		User user = (User) this.templateQLService.findObjectByQuery("user-getinfo", params, User.class);
		return user;
	}

}
